import os
import pandas as pd


def convert_row_to_text(row):
    return (
        f"### 一级分类: {row['一级分类']}\n"
        f"- **二级分类**: {row['二级分类']}\n"
        f"  - **关键词**: {row['关键词']}\n"
        f"  - **话术**: \"{row['话术']}\"\n\n"
    )


# 指定输入和输出文件夹
input_folder = 'ipt'
output_folder = 'opt'

# 获取ipt文件夹中的所有.xls文件
for filename in os.listdir(input_folder):
    if filename.endswith('.xls'):
        input_path = os.path.join(input_folder, filename)

        # 读取Excel文件
        df = pd.read_excel(input_path)

        # 打开文件准备写入
        output_filename = os.path.splitext(filename)[0] + '.txt'
        output_path = os.path.join(output_folder, output_filename)

        with open(output_path, 'w', encoding='utf-8') as file:
            # 遍历DataFrame中的每一行
            for index, row in df.iterrows():
                # 转换为文本格式
                text = convert_row_to_text(row)
                # 写入文件
                file.write(text)

print(f"转换完成，内容已保存到 '{output_folder}' 文件夹中。")